<?php

class MSSQLDataProvider extends DataProvider{
		var $db;
		var $user_id=null;
		var $cn;
		
		function MSSQLDataProvider($db_conf)
		{
			$this->cn = mssql_connect($db_conf['host'], $db_conf['username'],$db_conf['password'] );
			mssql_select_db($db_conf['dbname'],$this->cn);
		}
		
		function __destruct(){
			mssql_close($this->cn);
			
		}
		
		function setUserId($user_id){
			$this->user_id=$user_id;
		}
		
		function get_events($datestart,$datefinish,$type)
		{
			
			$sp = mssql_init("get_events"); 	
			mssql_bind($sp, "@datestart", gmdate("Y/m/d H:i:s",$datestart), SQLVARCHAR, false, false, 20);
			mssql_bind($sp, "@datefinish", gmdate("Y/m/d H:i:s",$datefinish), SQLVARCHAR, false, false, 20);
			mssql_bind($sp, "@user_id", $this->user_id, SQLINT4);
			$result=mssql_execute($sp);			
			while ($row = mssql_fetch_assoc($result))$events[]=$row;
			
			return $events;
		}
		
		function get_repeated_events($datestart,$type){
			$sp = mssql_init("get_repeated_events"); 	
			mssql_bind($sp, "@datestart", gmdate("Y/m/d H:i:s",$datestart), SQLVARCHAR, false, false, 20);
			mssql_bind($sp, "@user_id", $this->user_id, SQLINT4);
			$result=mssql_execute($sp);
			while ($row = mssql_fetch_assoc($result))$repeated_events[]=$row;
			return $repeated_events;
		}
		
		function get_event($event_id){
			$sp = mssql_init("get_event"); 	
			mssql_bind($sp, "@event_id", $event_id, SQLINT4);
			$result=mssql_execute($sp);
			return mssql_fetch_assoc($result);
			
		}
		
		function update_event($data,$event_id){
			$sp = mssql_init("update_event"); // stored proc name
			mssql_bind($sp, "@id", $event_id, SQLINT4);
			mssql_bind($sp, "@start", $data['start'], SQLVARCHAR, false, false, 20);
			mssql_bind($sp, "@finish", $data['finish'], SQLVARCHAR, false, false, 20);
			mssql_bind($sp, "@summary",$data['summary'], SQLVARCHAR, false, !isset($data['summary']), 200);
			mssql_bind($sp, "@description", $data['description'], SQLTEXT, false, !isset($data['description']));
			mssql_bind($sp, "@location", $data['location'], SQLVARCHAR,  false, !isset($data['location']),200);
			if(isset($data['allday'])) mssql_bind($sp, "@allday", $data['allday'], SQLBIT, false);
			mssql_bind($sp, "@calendar_id",$data['calendar_id'], SQLINT4, false, !isset($data['calendar_id']));
			mssql_execute($sp);
		}

		function delete_event($event_id){
			$sp = mssql_init("delete_event"); 	
			mssql_bind($sp, "@event_id", $event_id, SQLINT4);
			$result=mssql_execute($sp);
		}
		
		
		function insert_event($data){
			
			$sp = mssql_init("insert_event"); // stored proc name
			mssql_bind($sp, "@start", $data['start'], SQLVARCHAR, false, false, 20);
			mssql_bind($sp, "@finish", $data['finish'], SQLVARCHAR, false, false, 20);
			mssql_bind($sp, "@summary",$data['summary'], SQLVARCHAR, false, false, 200);
			mssql_bind($sp, "@description", $data['description'], SQLTEXT);
			mssql_bind($sp, "@location", $data['location'], SQLVARCHAR, false, false, 200);
			mssql_bind($sp, "@allday", $data['allday'], SQLBIT);
			mssql_bind($sp, "@calendar_id",$data['calendar_id'], SQLINT4);
			mssql_bind($sp, "RETVAL",$event_id, SQLVARCHAR);
			mssql_execute($sp);		
			return $event_id;
		}
		
		function delete_event_repeat($event_id){
			$sp = mssql_init("delete_event_repeat"); 	
			mssql_bind($sp, "@event_id", $event_id, SQLINT4);
			$result=mssql_execute($sp);	
		}
		
		function get_event_repeat($event_id){
			$sp = mssql_init("get_event_repeat"); 	
			mssql_bind($sp, "@event_id", $event_id, SQLINT4);
			$result=mssql_execute($sp);
			return mssql_fetch_assoc($result);
			
		}
		
		function set_event_repeat($data,$event_id)
		{
			
			$sp = mssql_init("edit_event_repeat"); // stored proc name
			mssql_bind($sp, "@type", $data['type'], SQLVARCHAR,false,false,200);
			mssql_bind($sp, "@repeat_finish", $data['repeat_finish'], SQLVARCHAR, false, is_null( $data['repeat_finish']), 20);
			mssql_bind($sp, "@frequency", $data['frequency'], SQLINT4);
			mssql_bind($sp, "@days", $data['days'], SQLVARCHAR, false,  is_null( $data['days']), 7);
			mssql_bind($sp, "@event_id",$event_id, SQLINT4);	
			mssql_execute($sp);
			
		}
		
		function get_calendars($user_id=null){
			if(is_null($user_id))$user_id=$this->user_id;
			$sp = mssql_init("get_calendars"); 	
			mssql_bind($sp, "@user_id", $this->user_id, SQLINT4);
			$result=mssql_execute($sp);			
			while ($row = mssql_fetch_assoc($result))$calendars[$row['id']]=$row;
			return $calendars;			
		}
		
		function insert_calendar($data){
			$sp = mssql_init("insert_calendar"); // stored proc name
			mssql_bind($sp, "@name", $data['name'], SQLVARCHAR, false, false, 50);
			mssql_bind($sp, "@bgcolor", $data['bgcolor'], SQLVARCHAR, false, false, 6);
			mssql_bind($sp, "@textcolor",$data['textcolor'], SQLVARCHAR, false, false, 6);
			mssql_bind($sp, "RETVAL",$calendar_id, SQLVARCHAR);
			mssql_execute($sp);
			$this->bind_calendar_to_user($calendar_id,$this->user_id);
		}
		
		function update_calendar($data,$calendar_id){
			
		}
		
		function delete_calendar($calendar_id){
			
		}
	
		function bind_calendar_to_user($calendar_id,$user_id){
			$sp = mssql_init("insert_calendar_user"); // stored proc name
			mssql_bind($sp, "@calendar_id", $calendar_id, SQLINT4);
			mssql_bind($sp, "@user_id", $user_id, SQLINT4);
			mssql_execute($sp);
		}
		
		
			
	}